//
//  TXTViewController.m
//  A手工客
//
//  Created by 萌萌的小坤坤 on 16/6/13.
//  Copyright © 2016年 冷坤. All rights reserved.
//

#import "TXTViewController.h"
#import "TXTCollectionView.h"
#import "TXTModel.h"
#import "AFNetworking.h"
#define TXTNET @"http://m.shougongke.com/index.php?&c=Course&a=newCourseList&gcate=allcate&order=new&vid=18"
#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height

#define COLORS [UIColor colorWithRed:(arc4random() % 256 / 255.0) green:(arc4random() % 256 / 255.0) blue:(arc4random() % 256 / 255.0) alpha:1.0];

@interface TXTViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic ,retain) UICollectionView *collectionView;
@property (nonatomic ,retain) NSMutableArray *arrTXT;

@end

@implementation TXTViewController


- (NSMutableArray *)arrTXT{
    if (!_arrTXT) {
        self.arrTXT = [NSMutableArray array];
    }
    return _arrTXT;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatCollectionView:self.view.bounds];
    
    
    [self getTXTUrl];
}
#pragma mark - 创建collectionView
- (void)creatCollectionView:(CGRect)frame{
    // 布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(345 / 2, 270);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    // 创建collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
    // 注册cell
    [self.collectionView registerClass:[TXTCollectionView class] forCellWithReuseIdentifier:@"TXTCollection"];
}

#pragma mark - 网络请求
- (void)getTXTUrl{

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil];
    
    [manager GET:TXTNET parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        
        for (NSDictionary *data in dic) {
            TXTModel *model = [TXTModel TXTModelWithDic:data];
            [self.arrTXT addObject:model];
        }
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];


}

#pragma mark - 实现collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrTXT.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TXTCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TXTCollection" forIndexPath:indexPath];
    cell.backgroundColor = COLORS;
    
    TXTModel *models = self.arrTXT[indexPath.row];
    cell.model = models;
    
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"$$$");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
